﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Ship002
{
    class Ship
    {
        GraphicsDevice device;

        public Vector3 position;


        private float moveSpeed = 0.5f;

        private float rotationSpeed = 0.05f;
        private float maxRotation = MathHelper.PiOver4;


        private Matrix world;
        private Matrix view;
        private Matrix projection;

        Model shipModel;
        Texture2D shipTexture;

        BasicEffect effect;

        float moveX = 0.0f;
        float moveZ = 0.0f;
        float rotateZ = 0.0f;

       

        public Ship(GraphicsDevice device, Model shipModel, Texture2D shipTexture, Matrix view, Matrix projection)
        {
            this.device = device;
            effect = new BasicEffect(device);
            this.shipModel = shipModel;
            this.shipTexture = shipTexture;
            this.view = view;
            this.projection = projection;

            UpdateModel();
            
        }


        public void DrawModel()
        {
            world = Matrix.CreateScale(0.01f) * Matrix.CreateRotationY(MathHelper.Pi) * Matrix.CreateRotationZ(rotateZ) * Matrix.CreateTranslation(position) ;

            foreach (ModelMesh mesh in shipModel.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    effect = (BasicEffect)part.Effect;
                    effect.World = world;
                    effect.View = view;
                    effect.Projection = projection;
                    effect.Texture = shipTexture;
                    effect.TextureEnabled = true;
                }
                mesh.Draw();
            }
        }


        public void UpdateModel()
        {
            KeyboardState keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Up))
            {
                moveZ -= moveSpeed;
            }
            if (keyState.IsKeyDown(Keys.Down))
            {
                moveZ += moveSpeed;
            }
            if (keyState.IsKeyDown(Keys.Left))
            {
                moveX -= moveSpeed;
                if (rotateZ > maxRotation)
                {
                    rotateZ = maxRotation;
                }
                else
                {
                    rotateZ += rotationSpeed;
                }
                

            }
            if (keyState.IsKeyDown(Keys.Right))
            {
                moveX += moveSpeed;
                if (rotateZ < -maxRotation)
                {
                    rotateZ = -maxRotation;
                }
                else
                {
                    rotateZ -= rotationSpeed;
                }

                
            }

            if (!(keyState.IsKeyDown(Keys.Right) | keyState.IsKeyDown(Keys.Left)))
            {
                if (rotateZ > 0)
                {
                    rotateZ -= rotationSpeed;
                }
                if (rotateZ < 0)
                {
                    rotateZ += rotationSpeed;
                }
            }

            position = new Vector3(moveX, 50, moveZ);

        }

    }
}
